import Vue from 'vue'
import VueI18n from '@/uni_modules/vue-i18n/vue-i18n.min.js'

Vue.use(VueI18n)

let lang = uni.getStorageSync('_lang')
if (!lang || lang === 'System') {
	const res = uni.getSystemInfoSync()
	lang = res.language
	uni.setStorageSync('_lang', lang)
}

const messages = {}
const locales = require.context('@/locale', true, /[A-Za-z0-9-_,\s]+\.json$/i)

locales.keys().forEach(key => {
  const matched = key.match(/([A-Za-z0-9-_]+)\./i)
  if (matched && matched.length > 1) {
    const locale = matched[1]
    messages[locale] = locales(key)
  }
})

export default new VueI18n({
	locale: lang,
	fallbackLocale: 'en',
	silentFallbackWarn: true,
	messages
});